package com.fieldchain.ex.fa100.ui.personal.activity;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.access.Constant;
import com.fieldchain.ex.fa100.base.MyBaseActivity;
import com.fieldchain.ex.fa100.bean.InviteBean;
import com.fieldchain.ex.fa100.bean.User;
import com.fieldchain.ex.fa100.ui.personal.adapter.InviteListAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.fieldchain.ex.fa100.utils.net.access.HttpRequest;
import com.fieldchain.ex.fa100.utils.net.modle.StringRequest;
import com.fieldchain.ex.fa100.utils.share.ShareManager;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/16
 */
public class InviteListActivity extends MyBaseActivity implements View.OnClickListener {
    private RelativeLayout reaBack;
    private TextView tvCenter;
    private SmartRefreshLayout refresh;
    private RecyclerView rv;
    private RelativeLayout reaEmpty;
    private TextView tvEmpty;
    private User user;
    private InviteListAdapter adapter;
    private int page = 1;
    private List<InviteBean> mList = new ArrayList<>();

    @Override
    public void findView() {
        reaBack = findViewById(R.id.rea_back);
        tvCenter = findViewById(R.id.tv_center);
        refresh = findViewById(R.id.refresh);
        rv = findViewById(R.id.rv);
        reaEmpty = findViewById(R.id.rea_empty);
        tvEmpty = findViewById(R.id.tv_empty);
    }

    @Override
    protected void onResume() {
        super.onResume();
        user = ShareManager.getInstance().getUser();
    }

    @Override
    public void setValue() {
        tvCenter.setText("邀请记录");
        tvEmpty.setText("亲，暂无邀请记录哦");
        reaBack.setOnClickListener(this);

        adapter = new InviteListAdapter(mContext);
        rv.setLayoutManager(new LinearLayoutManager(mContext));
        rv.setAdapter(adapter);

        getData();
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                refreshLayout.finishLoadMore(1000);
                getData();
            }
        });

        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                mList.clear();
                refresh.finishRefresh(1000);
                getData();
            }
        });

    }

    @Override
    public int getLayout() {
        return R.layout.activity_invitelist;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rea_back:
                finish();
                break;
        }
    }

    public void getData() {
        HashMap map = new HashMap();
        map.put("page", page + "");
        HttpRequest.getInstance().getRequest(map, Constant.getinvite,new StringRequest() {
            @Override
            public void onSucess(String str, int code, String msg) {
                List<InviteBean> list = null;
                try {
                    JSONArray aar = new JSONObject(str).getJSONObject("data").getJSONArray("data");
                    list = new Gson().fromJson(aar.toString(), new TypeToken<List<InviteBean>>() {}.getType());
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                if (code == 200&&list.size()>0) {
                    mList.addAll(list);
                }

                if (list.size() == 0) {
                    if (page > 1) {
                        page--;
                    }
                }
                if (mList.size() > 0) {
                    reaEmpty.setVisibility(View.GONE);
                } else {
                    reaEmpty.setVisibility(View.VISIBLE);
                }
                adapter.setData(mList);
            }

            @Override
            public void onFaild(Exception e) {
                if (page > 1) {
                    page--;
                }
                if (mList.size() > 0) {
                    reaEmpty.setVisibility(View.GONE);
                } else {
                    reaEmpty.setVisibility(View.VISIBLE);
                }
            }
        });
    }
}
